package top.lfyao.designpattern.details.factory.method;

import top.lfyao.designpattern.details.factory.simple.Operation;
import top.lfyao.designpattern.details.factory.simple.OperationAdd;
import top.lfyao.designpattern.details.factory.simple.OperationMultiplication;
import top.lfyao.designpattern.details.factory.simple.OperationSubtraction;

import java.util.HashMap;
import java.util.Map;

public class reflexOparation {
    private static Map<String, Class<?>> allOperationMaps = new HashMap<>();

    private static void fillMap() {
        allOperationMaps.put("+", AddFactory.class);
        allOperationMaps.put("-", SubtractionFactory.class);
        allOperationMaps.put("*", MultiplicationFactory.class);
        allOperationMaps.put("/", SubtractionFactory.class);
    }

    public static IFactory createOperation(String operator) {
        IFactory iFactory;
        try {
            fillMap();
            Class<?> operationClass = allOperationMaps.get(operator);

            if (operationClass == null) {
                throw new RuntimeException("unsupported operation");
            }
            iFactory = (IFactory) operationClass.newInstance();
            return iFactory;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
